package com.llap.exp190929.controller;

import com.llap.exp190929.dto.PaginatedDto;
import com.llap.exp190929.entity.Aqi;
import com.llap.exp190929.service.AqiService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Created by LLAP on 2019/10/14.
 * Copyright (c) 2019 LLAP. All rights reserved.
 */
@RestController
@RequestMapping("aqis")
@AllArgsConstructor
@CrossOrigin
@Slf4j
public class AqisController {
    private AqiService aqiService;

    @GetMapping("")
    public PaginatedDto<Aqi> findAQIs(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "20") Integer pageSize,
            @RequestParam(required = false, defaultValue = "-1") Integer districtId) {
        return PaginatedDto.from(aqiService.findAQIs(page, pageSize, districtId));
    }

    @GetMapping("{id}")
    public Aqi findAQI(@PathVariable Integer id) {
        return aqiService.findAQI(id);
    }

    @PostMapping("")
    public Aqi addAQI(@RequestBody Aqi aqi) {
        return aqiService.addAQI(aqi);
    }

    @DeleteMapping("")
    public void deleteAQI(@RequestParam Integer id) {
        aqiService.deleteAQI(id);
    }

    @PatchMapping("")
    public Aqi updateAQI(@RequestBody Aqi aqi) {
        return aqiService.updateAQI(aqi);
    }
}
